package Zoo.Impl;

import Zoo.Entity.LeaderEntity1;
import Zoo.Entity.ZoomenEntity;
import Zoo.Service.LeaderService1;
import Zoo.Util.TableDTO;
import lesson05.utils.JdbcUtils_DBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 * 1、园长的登录功能
 * 2、园长所看到的动物饲养员信息界面
 */

public class LeaderService1Impl implements LeaderService1 {
    //登录
    @Override
    public boolean validateLeader(LeaderEntity1 leaderEntity1) {
        String leaderName=leaderEntity1.getLeaderName();
        String idNumber= leaderEntity1.getIdNumeber();
        String sql ="SELECT `id_number` FROM `leaders` WHERE `name` =?";
        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            //获取数据库连接
            conn = JdbcUtils_DBCP.getConnection();
            if(conn==null){
                //如果没获取到数据库连接，则返回false
                return false;
            }
            st= conn.prepareStatement(sql);
            st.setString(1, leaderEntity1.getLeaderName());
            rs = st.executeQuery();
            while(rs.next()){
                String pwd = rs.getString(1);
                if(leaderEntity1.getIdNumeber().equals(pwd)){
                    //如果获取的身份证号是相对应的，则返回true
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils_DBCP.release(conn,st,rs);
        }



        //如果获取的用户名和身份证号是不匹配的，则返回false
        return false;
    }
    //查询
    @Override
    public TableDTO retrieveLeader1(LeaderEntity1 leaderEntity1) {
        StringBuilder sql=new StringBuilder();
        sql.append("SELECT * FROM `zoomen` ");
        if(leaderEntity1.getSearchKey()!=null && !"".equals(leaderEntity1.getSearchKey().trim())){
            sql.append("where `name` like '%"+leaderEntity1.getSearchKey().trim()+"%'");
        }

        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        //查出来的数据存在这里
        TableDTO returnDTO=new TableDTO();


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            rs = st.executeQuery();
            //存放连接数据库查出来的数据
            returnDTO.setData(fillData(rs));;


            return returnDTO;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,rs);
        }
        return null;
    }


    //从数据库查出来的数据
    private Vector<Vector<Object>> fillData(ResultSet rs) throws SQLException {
        Vector<Vector<Object>> data=new Vector<>();
        while(rs.next()){
            Vector<Object> oneRecord =new Vector<>();
            int id=rs.getInt("id");
            String idNumber = rs.getString("id_number");
            String phone = rs.getString("phone");
            String name = rs.getString("name");


            oneRecord.addElement(id);
            oneRecord.addElement(idNumber);
            oneRecord.addElement(phone);
            oneRecord.addElement(name);



            data.addElement(oneRecord);
        }
        return data;
    }

    @Override
    public boolean update(ZoomenEntity zoomenEntity) {
        StringBuilder sql=new StringBuilder();
        sql.append("UPDATE `zoomen` SET `id_number`=?,`phone`=?,`name`=? WHERE `id`=?");

        Connection conn=null;
        PreparedStatement st=null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setString(1,zoomenEntity.getIdNumeber());
            st.setString(2,zoomenEntity.getPhone());
            st.setString(3,zoomenEntity.getZoomenName());
            st.setInt(4,zoomenEntity.getId());



            return st.executeUpdate()==1;


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,null);
        }
        return false;
    }

    @Override
    public boolean delete(ZoomenEntity zoomenEntity) {
        StringBuilder sql=new StringBuilder();
        sql.append("delete from `zoomen`  WHERE `id`=?");

        Connection conn=null;
        PreparedStatement st=null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());

            st.setInt(1,zoomenEntity.getId());



            return st.executeUpdate()==1;


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,null);
        }
        return false;
    }
}
